<?php

namespace App\Models;

use App\Models\ComModel;

/**
 * 推荐位模型
 */
class FlagsModel extends ComModel
{

    public $tablename;

    public function __construct(...$param)
    {
        parent::__construct();
        $this->tablename = 'flags';
    }

    //新增和编辑全部数据时验证
    private function _validate($data, $id = 0)
    {
        if (!$data) {
            return ams_rt(1, '参数错误');
        }
        $data = esc($data);
        if (!$data['name']) {
            return ams_rt(1, '应用名称必须填写');
        }
        $flag = $this->field_exitsts($this->tablename, 'name', $data['name'], $id);
        if ($flag) {
            return ams_rt(1, '名称已存在');
        }
        $flag = $this->field_exitsts($this->tablename, 'code', $data['code'], $id);
        if ($flag) {
            return ams_rt(1, '标识已存在');
        }
        return ams_rt(0, 'ok', $data);
    }

    public function add($data)
    {
        $rt = $this->_validate($data);
        if ($rt['code']) {
            return $rt;
        }
        $data = $rt['data'];
        $this->db->table($this->tablename)->insert($data);
        $id = $this->db->insertID();
        if ($id) {
            return ams_rt(0, '操作成功', $id);
        }
        return ams_rt(1, '操作失败', $data);
    }

    public function edit($id, $data)
    {
        if (!$id) {
            return ams_rt(1, '参数错误');
        }
        $rt = $this->_validate($data, $id);
        if ($rt['code']) {
            return $rt;
        }
        $data = $rt['data'];

        $flag = $this->db->table($this->tablename)->where('id', $id)->update($data);
        if ($flag) {
            return ams_rt(0, '操作成功', $id);
        }
        return ams_rt(1, '操作失败', $udata);
    }

    public function get($id)
    {
        if (!$id) {
            return ams_rt(1, '参数错误');
        }
        $row = $this->db->table($this->tablename)->where('id', $id)->get()->getRowArray();
        if (!$row) {
            return [];
        }
        return $row;
    }

    public function del($id)
    {
        if (!$id) {
            return ams_rt(1, '参数错误');
        }
        $row = $this->db->table($this->tablename)->where('id', $id)->get()->getRowArray();
        if (!$row) {
            return ams_rt(1, '数据不存在', []);
        }
        $this->db->table($this->tablename)->where('id', $id)->delete();
        $this->db->table($this->tablename . '_data')->where('fid', $id)->delete();
        return ams_rt(0, '操作成功');
    }

    // 分页
    public function limit_page($page = 0, $size = 10, $total = 0, $param = [])
    {
        $page = max(1, (int) $page);
        $total = (int) $total;

        unset($param['page']);
        if ($param) {
            $param = esc($param);
        }

        if ($size > 0 && !$total) {
            $select = $this->db->table($this->tablename)->select('count(*) as total');
            $param = $this->_limit_where($select, $param);
            $query = $select->get();
            if (!$query) {
                log_message('error', '数据查询失败：' . $this->tablename);
                return [[], $total, $param];
            }
            $data = $query->getRowArray();
            $total = (int) $data['total'];
            $param['total'] = $total;
            unset($select);
            if (!$total) {
                return [[], $total, $param];
            }
        }

        $select = $this->db->table($this->tablename);
        $order = isset($param['order']) ? esc($param['order']) : '';
        $param = $this->_limit_where($select, $param);
        $size > 0 && $select->limit($size, $size * ($page - 1));
        $query = $select->orderBy($order)->get();
        if (!$query) {
            log_message('error', '数据查询失败：' . $this->tablename);
            return [[], $total, $param];
        }
        $data = $query->getResultArray();
        $param['order'] = $order;
        $param['total'] = $total;

        return [$data, $total, $param];

    }

    /**
     * 条件查询
     *
     * @param    object    $select    查询对象
     * @param    intval    $where    是否搜索
     * @return    intval
     */
    protected function _limit_where(&$select, $param)
    {
        // 条件搜索
        if ($param) {
            if (isset($param['id']) && $param['id']) {
                $select->where('id', (int) $$param['id']);
            }
            if (isset($param['name']) && $param['name']) {
                $select->like('name', urldecode($param['name']));
            }
            if (isset($param['code']) && $param['code']) {
                $select->like('code', urldecode($param['code']));
            }
        }
        return $param;
    }
    //
    public function getFlagsAll()
    {
        $rows = $this->db->table($this->tablename)->orderBy('id', 'desc')->get()->getResultArray();
        if (!$rows) {
            return [];
        }
        foreach ($rows as $v) {
            $data[$v['id']] = $v;
        }
        return $data;
    }


    //添加内容
    public function addContent($fid, $cid, $table)
    {
        if (!$fid || !$cid || !$table) {
            return ams_rt(1, '参数错误');
        }

        $fid = intval($fid);
        $flag = $this->db->table($this->tablename)->where('id', $fid)->get()->getRowArray();
        if (!$flag) {
            return ams_rt(1, '推荐位不存在');
        }

        $cid = intval($cid);
        $row = $this->db->table($this->tablename . '_data')->where('cid', $cid)->where('table', $table)->get()->getRowArray();
        if ($row) {
            return ams_rt(0, '操作成功');
        }
        $this->db->table($this->tablename . '_data')->insert(['fid' => $fid, 'cid' => $cid, 'table' => $table]);

    }

    //移除内容
    public function delContent($fid, $cid, $table)
    {
        if (!$fid || !$cid || !$table) {
            return ams_rt(1, '参数错误');
        }

        $fid = intval($fid);
        $flag = $this->db->table($this->tablename)->where('id', $fid)->get()->getRowArray();
        if (!$flag) {
            return ams_rt(1, '推荐位不存在');
        }

        $cid = intval($cid);
        $row = $this->db->table($this->tablename . '_data')->where('cid', $cid)->where('table', $table)->get()->getRowArray();
        if ($row) {
            $this->db->table($this->tablename . '_data')->where('cid', $cid)->where('table', $table)->delete();
        }
        return ams_rt($cid, '操作成功');
    }

}
